﻿namespace Journeo_backend.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

        public DbSet<Models.Domain.Attraction> Attraction { get; set; }
        public DbSet<Models.Domain.Region> Region { get; set; }
        public DbSet<Models.Domain.User> User { get; set; }
        public DbSet<Models.Domain.PlanAttraction> PlanAttraction { get; set; }
        public DbSet<Models.Domain.TravelPlan> TravelPlan { get; set; }
        public DbSet<Models.Domain.TransportationPlan> TransportationPlan { get; set; }
        public DbSet<Models.Domain.AttractionComment> AttractionComment { get; set; }
        public DbSet<Models.Domain.CommentLike> CommentLike { get; set; }
        public DbSet<Models.Domain.AttractionLike> AttractionLike { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Attraction>()
                .HasOne(a => a.Region)
                .WithMany()
                .HasForeignKey(a => a.RegionId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<PlanAttraction>()
                .HasOne(pa => pa.Attraction)
                .WithMany()
                .HasForeignKey(pa => pa.AttractionId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<PlanAttraction>()
                .HasOne(pa => pa.TravelPlan)
                .WithMany(tp => tp.PlanAttractions)
                .HasForeignKey(pa => pa.PlanId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Region>()
                .HasOne(r => r.ParentRegion)
                .WithMany()
                .HasForeignKey(r => r.ParentId);

            modelBuilder.Entity<TransportationPlan>()
                .HasOne(tp => tp.FromAttraction)
                .WithMany()
                .HasForeignKey(tp => tp.FromAttractionId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<TransportationPlan>()
                .HasOne(tp => tp.ToAttraction)
                .WithMany()
                .HasForeignKey(tp => tp.ToAttractionId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<TransportationPlan>()
                .HasOne(tp => tp.TravelPlan)
                .WithMany(tp => tp.TransportationPlans)
                .HasForeignKey(tp => tp.PlanId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<TravelPlan>()
                .HasOne(tp => tp.User)
                .WithMany(u => u.TravelPlans)
                .HasForeignKey(tp => tp.UserId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<AttractionComment>()
                .HasOne(ac => ac.Attraction)
                .WithMany()
                .HasForeignKey(ac => ac.AttractionId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<CommentLike>()
                .HasOne(cl => cl.Comment)
                .WithMany()
                .HasForeignKey(cl => cl.CommentId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<AttractionLike>()
                .HasOne(al => al.Attraction)
                .WithMany()
                .HasForeignKey(al => al.AttractionId)
                .OnDelete(DeleteBehavior.Cascade);
        }
    }
}
